function plsb=rpls()
    plsb=floor(rand(gsize,3*gsize-1)*3)-1;
    plsb(1,:)=floor(rand(1,3*gsize-1)*2);
    plsb(gsize,:)=floor(rand(1,3*gsize-1)*2)-1;
endfunction
function plsi=plsir(pls,w)
    plsi=pls;
    eq=1;
    //while eq>0
    for ns=1:100
        //printf("iter %d\n",eq);
        //calc v
        //TODO size changes
        A=zeros((3*gsize-1)*gsize,3*gsize*gsize);
        B=zeros((3*gsize-1)*gsize,1);
        for i=1:gsize
            for j=1:3*gsize-1
                A((i-1)*(3*gsize-1)+j,(i-1)*3*gsize+j)=1;
                a=pls(i,j);
                s=i+a;
                A((i-1)*(3*gsize-1)+j,(s-1)*3*gsize+j+1)=-gm;
                B((i-1)*(3*gsize-1)+j,1)=-rw(i-1,j-1,a,w);
            end
        end
        x0=linsolve(A,B);
        //calc new plsi
        for i=1:gsize
            for j=1:3*gsize-1
                plsi(i,j)=getact(i,j,w,x0);
            end
        end
        //st1=genst(plsi);
        //st2=genst(pls);
        //eq=sum(~(pls==plsi));
        pls=plsi
    end
endfunction
function a=getact(s,t,w,x0)
    if s==1 then
        r=zeros(2,1);
        r(1)=rw(s-1,t-1,0,w)+gm*x0((s-1)*3*gsize+t+1);//TODO t thingy
        r(2)=rw(s-1,t-1,1,w)+gm*x0(s*3*gsize+t+1);
        [mr,a]=max(r);
        a=a-1;
    elseif s==gsize
        r=zeros(2,1);
        r(1)=rw(s-1,t-1,-1,w)+gm*x0((s-2)*3*gsize+t+1);
        r(2)=rw(s-1,t-1,0,w)+gm*x0((s-1)*3*gsize+t+1);
        [mr,a]=max(r);
        a=a-2;
    else
        r=zeros(3,1);
        r(1)=rw(s-1,t-1,-1,w)+gm*x0((s-2)*3*gsize+t+1);
        r(2)=rw(s-1,t-1,0,w)+gm*x0((s-1)*3*gsize+t+1);
        r(3)=rw(s-1,t-1,1,w)+gm*x0(s*3*gsize+t+1);
        [mr,a]=max(r);
        a=a-2;
    end
    //determine best action
endfunction
function r=rw(s,t,a,w)
    y=genf(s+a,t+1,a);
    //disp(w);
    //disp(size(y,1));
    //r=1;
    r=(y')*w;
endfunction
function s=genst(plo)
    s=zeros(3*gsize,1);
    for i=2:3*gsize
        s(i)=s(i-1)+plo(s(i-1)+1,i-1);
    end
endfunction
function z=br(w)
    s=0;
    t=0;
    //sa=0;
    z=zeros(3*gsize-1,1);
    while (t<3*gsize-1) 
        a=getract(s,t,w,H);
        s=s+a;
        t=t+1;
        z(t)=a;
        //sa=sa+abs(a);
    end
    //z(t+1)=a;
endfunction
function a=getract(s,t,w,d)
    r=zeros(3,1);
    for j=1:3
        r(j)=rw2(s,t,j-2,w,d);
        //printf("%f ",r(j));
    end
    //printf("\n");
    if s==0 then
        [mr,a]=max(r(2:3));
        a=a+1
    elseif s==gsize-1
        [mr,a]=max(r(1:2));
    else
        [mr,a]=max(r);
        //a=a+1;
    end
    //[mr,a]=max(r);
    a=a-2;
endfunction
function r=rw2(s,t,a,w,d)
    r=0;
    rc=zeros(3,1);
    //printf("%d %d %d %d\n",s,t,a,d)
    if d>0 & s>=0 & s<gsize then

        r=rw(s,t,a,w);
        for j=1:3
            rc(j)=rw2(s+a,t+1,j-2,w,d-1);
        end
        if s==0 then
            mr=max(rc(2:3));
        elseif s==gsize-1
            mr=max(rc(1:2));
        else
            mr=max(rc);
        end
            //printf("%d\n",mr);
        r=r+mr;
    end
    //disp(w);
    //disp(size(y,1));
    //r=1;
    //r=(y')*w;
endfunction
